package cn.iocoder.yudao.module.social.controller.admin.interaction;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.module.social.controller.admin.interaction.vo.*;
import cn.iocoder.yudao.module.social.convert.interaction.InteractionConvert;
import cn.iocoder.yudao.module.social.dal.dataobject.interaction.InteractionDO;
import cn.iocoder.yudao.module.social.service.interaction.InteractionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 互动")
@RestController
@RequestMapping("/social/interaction")
@Validated
public class InteractionController {

    @Resource
    private InteractionService interactionService;

    @PostMapping("/create")
    @Operation(summary = "创建互动")
    @PreAuthorize("@ss.hasPermission('social:interaction:create')")
    public CommonResult<Long> createInteraction(@Valid @RequestBody InteractionCreateReqVO createReqVO) {
        return success(interactionService.createInteraction(createReqVO));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除互动")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('social:interaction:delete')")
    public CommonResult<Boolean> deleteInteraction(@RequestParam("id") Long id) {
        interactionService.deleteInteraction(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得互动")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('social:interaction:query')")
    public CommonResult<InteractionRespVO> getInteraction(@RequestParam("id") Long id) {
        InteractionDO interaction = interactionService.getInteraction(id);
        return success(InteractionConvert.INSTANCE.convert(interaction));
    }

    @GetMapping("/page")
    @Operation(summary = "获得互动分页")
    @PreAuthorize("@ss.hasPermission('social:interaction:query')")
    public CommonResult<PageResult<InteractionRespVO>> getInteractionPage(@Valid InteractionPageReqVO pageReqVO) {
        PageResult<InteractionDO> pageResult = interactionService.getInteractionPage(pageReqVO);
        return success(InteractionConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出互动 Excel")
    @PreAuthorize("@ss.hasPermission('social:interaction:export')")
    public void exportInteractionExcel(@Valid InteractionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InteractionDO> list = interactionService.getInteractionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "互动.xls", "数据", InteractionRespVO.class,
                        InteractionConvert.INSTANCE.convertList(list));
    }

}
